package cf.huzpsb.lias;

import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;

import java.io.File;
import java.nio.file.Files;

public final class Main extends Plugin {
    public static int save;
    public static String reason;
    public static String hub;
    private static ConfigurationProvider banpvd;
    private static boolean compress;
    private static Configuration config;
    private static Configuration bancfg;
    private static File banfile;

    public static void save() {
        try {
            banpvd.save(bancfg, banfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean getstat(String node) {
        if (!bancfg.contains(node)) {
            return false;
        }
        return bancfg.getString(getfather(node)).equals("ban");
    }

    public static String getfather(String node) {
        if (!bancfg.contains(node)) {
            return "error";
        } else {
            String s = bancfg.getString(node);
            if (s.equals("ban") || s.equals("point")) {
                return node;
            } else {
                String compress;
                compress = getfather(s);
                if (Main.compress) {
                    bancfg.set(node, compress);
                }
                return compress;
            }
        }
    }

    public static void merge(String from, String to) {
        if (!bancfg.contains(from)) {
            bancfg.set(from, "point");
        }
        if (!bancfg.contains(to)) {
            bancfg.set(to, "point");
        }
        if (!getfather(from).equals(getfather(to))) {
            if (getstat(from)) {
                bancfg.set(getfather(to), getfather(from));
            } else {
                bancfg.set(getfather(from), getfather(to));
            }
        }
    }

    public static void setstat(String node, boolean banned) {
        if (banned) {
            bancfg.set(getfather(node), "ban");
        } else {
            bancfg.set(getfather(node), "point");
        }
    }

    @Override
    public void onEnable() {
        this.getLogger().info("§c欢迎使用Liasban! 作者:huzpsb");
        this.getLogger().info("§c插件正在初始化,请稍候...");
        this.initCFG();
        this.installCMD();
        this.getProxy().getPluginManager().registerListener(this, new PlayerListener());
        this.getLogger().info("§c插件已经启动!感谢您使用本插件!");
    }

    @Override
    public void onDisable() {
        this.getLogger().info("§c感谢您使用Liasban! 作者:huzpsb");
        this.getLogger().info("§c开始保存封禁列表与并查集...");
        save();
        this.getLogger().info("§c保存成功!插件即将关闭!期待我们还能再会!");
    }

    private void initCFG() {
        try {
            if (!this.getDataFolder().exists()) {
                this.getDataFolder().mkdir();
            }
            File config = new File(this.getDataFolder(), "config.yml");
            if (!config.exists()) {
                Files.copy(this.getResourceAsStream("config.yml"), config.toPath());
            }
            Main.config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(new File(this.getDataFolder(), "config.yml"));
            File ban = new File(this.getDataFolder(), "lias.yml");
            if (!ban.exists()) {
                Files.copy(this.getResourceAsStream("lias.yml"), ban.toPath());
            }
            banpvd = ConfigurationProvider.getProvider(YamlConfiguration.class);
            banfile = new File(this.getDataFolder(), "lias.yml");
            bancfg = banpvd.load(banfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        save = config.getInt("autosave");
        if (save != -1) {
            this.getLogger().info("§c自动保存系统正在启动...");
            AutoSaver stsk = new AutoSaver();
            Thread sthr = new Thread(stsk);
            sthr.start();
            this.getLogger().info("§c自动保存系统启动成功!");
        } else {
            this.getLogger().info("§c您禁用了自动保存系统!");
        }
        reason = config.getString("msg").replace("&n", "\n").replace("&", "§");
        compress = config.getBoolean("compress");
        hub = config.getString("hub");
    }

    private void installCMD() {
        this.getProxy().getPluginManager().registerCommand(this, new BanCMD("lias", "lias.admin", "lsfj"));
        this.getProxy().getPluginManager().registerCommand(this, new MergeCMD("merge", "lias.merge", "gl"));
        this.getProxy().getPluginManager().registerCommand(this, new GetCMD("query", "lias.query", "cx"));
    }
}
